package com.astronomy.recruit.controller;

import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.astronomy.recruit.pojo.Recruit;
import com.astronomy.recruit.service.RecruitService;
import com.astronomy.states.Status;
import com.github.pagehelper.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/recruit")
@CrossOrigin
public class RecruitController {


    @Autowired
    private RecruitService recruitService;

    @GetMapping
    public Result findAll(){
        List<Recruit> recruitList = recruitService.findAll();
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS,recruitList) ;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Recruit recruit = recruitService.findById(id);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,recruit);
    }

    @PostMapping
    public Result add(@RequestBody Recruit recruit){
        recruitService.add(recruit);
        return new Result(true,StatusCode.OK,Status.ADD_SUCCESS);
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody Recruit recruit, @PathVariable String id){
        recruit.setId(id);
        recruitService.update(recruit);
        return new Result(true,StatusCode.OK,Status.UPDATE_SUCCESS);
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        recruitService.delete(id);
        return new Result(true,StatusCode.OK,Status.DELETE_SUCCESS);
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        Page<Recruit> pageList = recruitService.findPage(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }

    @PostMapping(value = "/search" )
    public Result findList(@RequestBody Map searchMap){
        List<Recruit> list = recruitService.findList(searchMap);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,list);
    }

    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody Map searchMap, @PathVariable  int page, @PathVariable  int size){
        PageResult pageResult = recruitService.findPage(searchMap, page, size);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,pageResult);
    }
    @GetMapping("/search/recommend")
    public Result recommend(){
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,recruitService.recommend(Status.STATE));
    }
    @GetMapping("/search/newlist")
    public Result newlist(){
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,recruitService.newList(Status.CLOSE));
    }

}
